package com.ruoyi.web.sso.controller;



import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.web.sso.domain.Area;
import com.ruoyi.web.sso.domain.Menu;
import com.ruoyi.web.sso.domain.Role;
import com.ruoyi.web.sso.domain.StaffRole;
import com.ruoyi.web.sso.domain.vo.AddRole;
import com.ruoyi.web.sso.service.IRoleService;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author cdd
 * @since 2021-10-20
 */
@Controller
@RequestMapping("/sso/role")
@Api(tags = "角色信息")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService roleService;

    @PostMapping("/add")
    @ApiOperation(value="新增角色",notes="新增角色")
    @ResponseBody
    public AjaxResult add(@RequestBody AddRole addRole)
    {
        return roleService.add(addRole);
    }



    @RequestMapping(value="/list",method= RequestMethod.POST)
    @ApiOperation(value="查询指定园区角色列表",notes="查询指定园区角色列表")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = Menu.class),
    })
    @ResponseBody
    public TableDataInfo list(@ApiParam(value = "系统ID")  String platformId,@ApiParam(value = "园区")  String areaId)
    {
        Role role = new Role();
        role.setPlatformId(platformId);
        role.setAreaId(areaId);
        startPage();
        List<Role> list = roleService.selectRoleList(role);
        return getDataTable(list);
    }


    @RequestMapping(value="/addStaffRole",method= RequestMethod.POST)
    @ApiOperation(value="新增用户角色分配",notes="新增用户角色分配")
    @ResponseBody
    public AjaxResult add(@RequestBody StaffRole sosStaffRole) throws  Exception
    {
        return roleService.addStaffRole(sosStaffRole);
    }



    /**
     * 修改保存角色
     */
    @PostMapping("/edit")
    @ResponseBody
    @ApiOperation(value="修改角色",notes="修改角色")
    public AjaxResult editSave(@Validated  Role role)
    {
        return AjaxResult.success(roleService.updateRole(role));
    }



    /**
     * 删除用户关联权限
     * @return
     */
    @PostMapping("/delRole")
    @ResponseBody
    @ApiOperation(value="删除用户权限",notes="删除用户权限")
    public AjaxResult delRole(String roleId)
    {
        return AjaxResult.success(roleService.delRole(roleId));
    }

    /**
     * 删除用户关联权限  - 角色，角色用户，角色菜单
     * @return
     */
    @PostMapping("/delAllRole")
    @ResponseBody
    @ApiOperation(value="删除用户关联权限",notes="删除用户权限")
    public AjaxResult delAllRole(String roleId)
    {
        return AjaxResult.success(roleService.delAllRole(roleId));
    }

    /**
     * 批量选择用户授权
     */
    @PostMapping("/insertAuthStaffAll")
    @ResponseBody
    @ApiOperation(value="批量选择用户授权",notes="批量选择用户授权")
    public AjaxResult insertAuthStaffAll(String roleId, String staffIds,String platformId)
    {
        return roleService.insertAuthStaffAllV2(roleId, staffIds, platformId);
    }

    /**
     * 删除用户权限
     * @param staffRole
     * @return
     */
    @PostMapping("/delStaffRole")
    @ResponseBody
    @ApiOperation(value="删除用户权限",notes="删除用户权限")
    public AjaxResult delStaffRole(StaffRole staffRole)
    {
        return roleService.deleteStaffRoleInfo(staffRole);
    }

    /**
     * 删除用户权限
     * @param staffRole
     * @return
     */
    @PostMapping("/cancelStaffRole")
    @ResponseBody
    public AjaxResult cancelStaffRole(StaffRole staffRole)
    {
        return toAjax(roleService.cancelStaffRole(staffRole));
    }

    /**
     * 批量删除用户权限
     * @return
     */
    @PostMapping("/cancelStaffRoles")
    @ResponseBody
    @ApiOperation(value="批量删除用户权限",notes="批量删除用户权限")
    public AjaxResult deleteStaffRoleInfos(String roleId, String staffIds)
    {
        return toAjax(roleService.deleteStaffRoleInfos(roleId,Convert.toStrArray(staffIds)));
    }

    /**
     * 查询园区id的列表
     */
    @PostMapping("/areaIdAndPlatformIdByRoleList")
    @ResponseBody
    @ApiOperation(value="查询园区id的列表",notes="查询园区id的列表")
    public  List<Role> areaIdAndPlatformIdByRoleList( String areaId,  String platformId)
    {
        Role role = new Role();
        role.setAreaId(areaId);
        role.setPlatformId(platformId);
        List<Role> list= roleService.selectRoleList(role);
        return list;
    }

    /**
     * 查询园区id的列表
     */
    @PostMapping("/platformIdAndAreaIdZeroByRoleList")
    @ResponseBody
    @ApiOperation(value="查询园区id的列表",notes="查询园区id的列表")
    public  List<Role> platformIdAndAreaIdZeroByRoleList( String areaId,  String platformId)
    {
        Role role = new Role();
        role.setAreaId("0");
        role.setPlatformId(platformId);
        List<Role> list= roleService.selectRoleList(role);
        return list;
    }

    /**
     * 分配园区角色
     */
    @PostMapping("/addAreaRole")
    @ResponseBody
    @ApiOperation(value="分配园区角色",notes="分配园区角色")
    public  AjaxResult addAreaRole( String roleId, String areaId)
    {
        return  toAjax(roleService.addAreaRole(roleId, areaId));
    }
    /**
     * 分配园区角色
     */
    @PostMapping("/setDefault")
    @ResponseBody
    @ApiOperation(value="分配园区角色",notes="分配园区角色")
    public  AjaxResult setDefault( String roleId,  String staffId)
    {
        return  toAjax(roleService.setDefault(roleId,staffId));
    }




    @GetMapping("/templateRole")
    @ApiOperation(value="模板角色页面",notes="模板角色页面")
    public String templateRole()
    {
        return "sso/role/role";
    }
}
